📌 當你一買到玩具,它就會自動貼上名字標籤
在物件被建立的時候就會自動執行
void 或 int)📌 當玩具壞掉被丟掉之前,它會自動說一句掰掰
在物件被刪除的時候自動執行
~
#include <iostream>
using namespace std;
class Student 
{
	private:
	    string name;
	    int age;
	public:
	    Student(string n, int a) 
	    {
	        name = n;
	        age = a;
	        cout << "學生: " << name << endl;
	    }
	    void say() 
	    {
	        cout << name << " " << age << " 歲" << endl;
	    }
	    ~Student() 
	    {
	        cout << "資料已刪除" << endl;
	    }
};
int main() 
{
    Student s("小A", 12);
    s.say();
    return 0;
}
建構子與解構子
幫助我們在物件出生和消失的處理
建構子 → 物件建立時,能擁有正確的初始狀態
解構子 → 確保物件消失時能妥善釋放資源,避免記憶體浪費或程式錯誤
📌這像是人生一樣,有出生,便有著離開